package cn.tinlan.shopcoin.app.search;



import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.DBManager;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.base.BaseActivity;
import co.lujun.androidtagview.TagContainerLayout;
import co.lujun.androidtagview.TagView;

/**
 * Created by Mortimer on 2018/11/26. 16:50
 * 佛祖🙏，永无bug。
 */
public class SearchActivity extends BaseActivity {

    @BindView(R.id.et_search)
    EditText met_search;
    @BindView(R.id.search_sure_btn)
    TextView mOK;

    @BindView(R.id.search_default)
    View defaultDisplayView;
    @BindView(R.id.search_history_data)
    TagContainerLayout mTags;
    @BindView(R.id.mSearchRV)
     RecyclerView mSearchRV;

    private SeachRecordAdapter searchListAdapter;
    private DBManager dbManager;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        super.initView();
        if (dbManager == null){
            dbManager = new DBManager(this);
        }
        initNavigationView();
        initTagsView();
        initRView();

    }

    private void initTagsView() {

        mTags.setTags(dbManager.queryData(""));
        mTags.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(int position, String text) {

            }

            @Override
            public void onTagLongClick(int position, String text) {

            }

            @Override
            public void onTagCrossClick(int position) {

            }
        });
    }

    private void initNavigationView() {
        met_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                defaultDisplayView.setVisibility(View.GONE);
                mSearchRV.setVisibility(View.VISIBLE);

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (met_search.getText().toString().trim().length() == 0){
                    mSearchRV.setVisibility(View.GONE);
                    defaultDisplayView.setVisibility(View.VISIBLE);
//                    met_search.clearFocus();
                }
            }
        });

        mOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (met_search.getText().toString().trim().length() != 0){
                    boolean hasData = dbManager.hasData(met_search.getText().toString().trim());
                    if (!hasData){
                        dbManager.insertData(met_search.getText().toString().trim());
                    }else {

                        ToastUtils.show("该内容已在历史记录中");
                    }

                    //
                    searchListAdapter.updata(dbManager.queryData(""));

                }else {

                    ToastUtils.show("\"请输入内容\"");
                }
            }
        });
    }

    private void initRView() {

        mSearchRV.setLayoutManager(new LinearLayoutManager(this));
        searchListAdapter = new SeachRecordAdapter(dbManager.queryData(""),this);
        mSearchRV.setAdapter(searchListAdapter);
    }

    @Override
    public void initData() {
        super.initData();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
            View v=getCurrentFocus();
            boolean  hideInputResult =isShouldHideInput(v,ev);
            Log.v("hideInputResult","zzz-->>"+hideInputResult);
            if(hideInputResult){
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) SearchActivity.this
                        .getSystemService(Activity.INPUT_METHOD_SERVICE);
                if(v != null){
                    if(imm.isActive()){
                        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            //之前一直不成功的原因是,getX获取的是相对父视图的坐标,getRawX获取的才是相对屏幕原点的坐标！！！
            Log.v("leftTop[]","zz--left:"+left+"--top:"+top+"--bottom:"+bottom+"--right:"+right);
            Log.v("event","zz--getX():"+event.getRawX()+"--getY():"+event.getRawY());
            if (event.getRawX() > left && event.getRawX() < right
                    && event.getRawY() > top && event.getRawY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }



}
